package com.cuixichang.minimalism.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cuixichang.minimalism.core.mybatis.cnstants.BaseFieldNames;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import com.cuixichang.minimalism.mapper.IRolePermissionMapper;
import com.cuixichang.minimalism.transfer.dto.IRolePermissionDTO;
import com.cuixichang.minimalism.transfer.factory.IRolePermissionAssembler;
import com.cuixichang.minimalism.transfer.po.IRolePermission;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.time.LocalDateTime;
import java.util.*;
import java.util.function.BiConsumer;
import java.util.stream.Collectors;
import java.util.function.Function;
import org.springframework.stereotype.Service;

/**
 * 系统角色权限关系表表服务实现类
 *
 * @author cuixichang
 * @date 2024-12-18
 */
@Service
public class IRolePermissionServiceImpl implements IRolePermissionService {

    @Autowired
    private IRolePermissionMapper iRolePermissionMapper;

    @Override
    public IRolePermissionDTO selectById(Long id){
        IRolePermission iRolePermission = iRolePermissionMapper.selectById(id);
        return IRolePermissionAssembler.INSTANCT.convertDto(iRolePermission);
    }

    @Override
    public IRolePermissionDTO selectOne(IRolePermissionDTO iRolePermissionDTO){
        Assert.notNull(iRolePermissionDTO,"参数为空,请检查");
        IRolePermission iRolePermission = IRolePermissionAssembler.INSTANCT.convertPo(iRolePermissionDTO);
        IRolePermission result = iRolePermissionMapper.selectOne(new QueryWrapper<>(iRolePermission));
        return IRolePermissionAssembler.INSTANCT.convertDto(result);
    }

    @Override
    public List<IRolePermissionDTO> selectByIds(List<Long> ids) {
        Assert.notEmpty(ids,"参数为空,请检查");
        List<IRolePermission> results = iRolePermissionMapper.selectBatchIds(ids);
        return results.stream().map(IRolePermissionAssembler.INSTANCT::convertDto).collect(Collectors.toList());
    }

    @Override
    public List<IRolePermissionDTO> selectList(IRolePermissionDTO iRolePermissionDTO){
        return this.selectList(iRolePermissionDTO, (iRolePermission, queryWrapper) -> {});
    }

    @Override
    public List<IRolePermissionDTO> selectList(IRolePermissionDTO iRolePermissionDTO, BiConsumer<IRolePermission, QueryWrapper<IRolePermission>> consumer){
        Assert.notNull(iRolePermissionDTO,"参数为空,请检查");
        IRolePermission iRolePermission = IRolePermissionAssembler.INSTANCT.convertPo(iRolePermissionDTO);
        QueryWrapper<IRolePermission> queryWrapper = new QueryWrapper<>(iRolePermission);
        if(Objects.nonNull(consumer)){
            consumer.accept(iRolePermission, queryWrapper);
        }
        queryWrapper.orderBy(true,true,BaseFieldNames.SORT).orderBy(true,false,BaseFieldNames.UPDATE_TIME);
        List<IRolePermission> results = iRolePermissionMapper.selectList(queryWrapper);
        return results.stream().map(IRolePermissionAssembler.INSTANCT::convertDto).collect(Collectors.toList());
    }

    @Override
    public Page<IRolePermissionDTO> selectPage(IRolePermissionDTO iRolePermissionDTO,long pageSize,long pageNo){
        return this.selectPage(iRolePermissionDTO, pageSize, pageNo, (iRolePermission, queryWrapper) -> {});
    }

    @Override
    public Page<IRolePermissionDTO> selectPage(IRolePermissionDTO iRolePermissionDTO,long pageSize,long pageNo, BiConsumer<IRolePermission, QueryWrapper<IRolePermission>> consumer){
        Assert.notNull(iRolePermissionDTO,"参数为空,请检查");
        IRolePermission iRolePermission = IRolePermissionAssembler.INSTANCT.convertPo(iRolePermissionDTO);
        QueryWrapper<IRolePermission> queryWrapper = new QueryWrapper<>(iRolePermission);
        if(Objects.nonNull(consumer)) consumer.accept(iRolePermission, queryWrapper);
        queryWrapper.orderBy(true,true,BaseFieldNames.SORT).orderBy(true,false,BaseFieldNames.UPDATE_TIME);
        Page<IRolePermission> page = iRolePermissionMapper.selectPage(new Page<>(pageNo,pageSize),queryWrapper);
        Page<IRolePermissionDTO> result = Page.of(page.getCurrent(),page.getSize(),page.getTotal());
        result.setRecords(page.getRecords().stream()
                .map(IRolePermissionAssembler.INSTANCT::convertDto).collect(Collectors.toList()));
        return result;
    }

    @Override
    public int deleteById(Long id){
        return iRolePermissionMapper.deleteById(id);
    }

    @Override
    public int deleteByIds(List<Long> ids){
        return iRolePermissionMapper.deleteBatchIds(ids);
    }

    @Override
    public int deleteByCondition(IRolePermissionDTO iRolePermissionDTO){
        return deleteByCondition(iRolePermissionDTO, (iRolePermission, queryWrapper)-> {});
    }

    @Override
    public int deleteByCondition(IRolePermissionDTO iRolePermissionDTO, BiConsumer<IRolePermission, QueryWrapper<IRolePermission>> consumer){
        Assert.notNull(iRolePermissionDTO,"参数为空,请检查");
        IRolePermission iRolePermission = IRolePermissionAssembler.INSTANCT.convertPo(iRolePermissionDTO);
        QueryWrapper<IRolePermission> queryWrapper = new QueryWrapper<>(iRolePermission);
        if(Objects.nonNull(consumer)) {
            consumer.accept(iRolePermission, queryWrapper);
        }
        return iRolePermissionMapper.delete(queryWrapper);
    }

    @Override
    public IRolePermissionDTO insert(IRolePermissionDTO iRolePermissionDTO){
        return insertHasOption(iRolePermissionDTO, (IRolePermissionDTO) -> null);
    }

    @Override
    public IRolePermissionDTO insertHasOption(IRolePermissionDTO iRolePermissionDTO, Function<IRolePermissionDTO,IRolePermissionDTO> preOption){
        Assert.notNull(iRolePermissionDTO,"参数为空,请检查");
        iRolePermissionDTO.check();
        IRolePermission iRolePermission = IRolePermissionAssembler.INSTANCT.convertPo(iRolePermissionDTO);
        Long createLoginId = iRolePermission.getCreateLoginId();
        Assert.notNull(createLoginId,"创建人ID为空,请检查");
        iRolePermission.setUpdateLoginId(createLoginId);
        LocalDateTime localDateTime = LocalDateTime.now();
        iRolePermission.setCreateTime(localDateTime);
        iRolePermission.setUpdateTime(localDateTime);
        Boolean isEnable = iRolePermission.getIsEnable();
        if(Objects.isNull(isEnable)) iRolePermission.setIsEnable(true);
        Integer sort = iRolePermission.getSort();
        if(Objects.isNull(sort)) iRolePermission.setSort(0);
        if(Objects.nonNull(preOption)){
            IRolePermissionDTO apply = preOption.apply(iRolePermissionDTO);
            if(Objects.nonNull(apply)) {
              return apply;
            }
        }
        int insert = iRolePermissionMapper.insert(iRolePermission);
        Assert.isTrue(insert>0,"插入失败");
        return IRolePermissionAssembler.INSTANCT.convertDto(iRolePermission);
    }

    @Override
    public int updateById(IRolePermissionDTO iRolePermissionDTO){
         return updateById(iRolePermissionDTO, (iRolePermission, updateWrapper) -> {});
    }

    @Override
    public int updateById(IRolePermissionDTO iRolePermissionDTO, BiConsumer<IRolePermission, UpdateWrapper<IRolePermission>> consumer){
        Assert.notNull(iRolePermissionDTO,"参数为空,请检查");
        Long id = iRolePermissionDTO.getId();
        Assert.notNull(id,"主键为空,请检查");
        IRolePermission iRolePermission = IRolePermissionAssembler.INSTANCT.convertPo(iRolePermissionDTO);
        Long createLoginId = iRolePermission.getCreateLoginId();
        Assert.notNull(createLoginId,"创建人ID为空,请检查");
        iRolePermission.setCreateLoginId(null);
        iRolePermission.setUpdateLoginId(createLoginId);
        iRolePermission.setUpdateTime(LocalDateTime.now());
        UpdateWrapper<IRolePermission> updateWrapper = new UpdateWrapper<IRolePermission>().eq(BaseFieldNames.ID, id);
        if(Objects.nonNull(consumer)) consumer.accept(iRolePermission,updateWrapper);
        return iRolePermissionMapper.update(iRolePermission,updateWrapper);
    }
}
